﻿//===================================================================================
// $$
//
//
// Code By: Code Milker V2
// Date On: 2013//
//===================================================================================


namespace Forest.Lion.Data.SqlServer.Repositories {
    using System;

    using System.Collections.Generic;

    using System.Data;

    using System.Linq;
    using Forest.Lion.Model;

    public class MessageTemplateRepository : BaseRepository, IMessageTemplateRepository {
        public MessageTemplateRepository(IUnitOfWork unitOfWork)
            : base(unitOfWork) {
        }
        public void Create(MessageTemplate messageTemplate) {
            this.GetDbSet<MessageTemplate>().Add(messageTemplate);
            this.UnitOfWork.SaveChanges();
        }

        public MessageTemplate GetMessageTemplate(Guid messageTemplateId) {
            return this.GetDbSet<MessageTemplate>()
                .Where(c => c.Id == messageTemplateId)
                .Single();
        }

        public void Update(MessageTemplate updatedMessageTemplate) {
            this.GetDbSet<MessageTemplate>().Attach(updatedMessageTemplate);
            this.SetEntityState(updatedMessageTemplate, updatedMessageTemplate.Id == Guid.Empty
                ? EntityState.Added
                : EntityState.Modified);
            this.UnitOfWork.SaveChanges();
        }

        public void Delete(Guid messageTemplateId) {
            MessageTemplate messageTemplateToDelete = this.GetMessageTemplate(messageTemplateId);
            this.GetDbSet<MessageTemplate>().Remove(messageTemplateToDelete);
            this.UnitOfWork.SaveChanges();
        }

        public IEnumerable<MessageTemplate> Find() {
            return this.GetDbSet<MessageTemplate>().Take(100)
                .ToList();
        }

        public IEnumerable<MessageTemplate> GetAll() {
            return this.GetDbSet<MessageTemplate>().Take(100)
                .ToList();
        }

        public IEnumerable<MessageTemplate> GetMessageTemplatesForCompany(Guid companyId) {
            return this.GetDbSet<MessageTemplate>()
                .Where(m => m.CompanyId == companyId)
                .ToList();
        }
    }
}
